﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameBase;
using Microsoft.Xna.Framework.Content;
using System.Xml;
using Microsoft.Xna.Framework;
using GameLayer;

namespace MainGame.GameForm
{
    public class FormControl : VisibleGameEntity<ButtonControl>
    {
        public int Margin { get; set; }
        public ContentManager Content { get; set; }
        public ButtonControl ActiveButton { get; set; }
        public override bool ListenEvent
        {
            get
            {
                return base.ListenEvent;
            }
            set
            {
                base.ListenEvent = value;
                foreach (var button in this.Items)
                {
                    button.ListenEvent = value;
                }
            }
        }
        public override Microsoft.Xna.Framework.Vector2 Position
        {
            get
            {
                return base.Position;
            }
            set
            {
                Vector2 d = value - Position;
                foreach (var item in this.Items)
                {
                    item.Position += d;
                }
                base.Position = value;
            }
        }
        public override Vector2 Size
        {
            get
            {
                return _size;
            }
            set
            {
                _size = value;
            }
        }
        protected Sprite Background { get; set; }
        public FormControl(ContentManager content)
        {
            this.Content = content;
            this.ListenEvent = true;
        }


        public virtual void ValidateUI()
        {
        }

        protected override void UpdateEntitys(Microsoft.Xna.Framework.GameTime gameTime, object extraData)
        {
            base.UpdateEntitys(gameTime, extraData);
            if (Background != null)
            {
                Background.Update(gameTime);
                Background.NextFrame();

            }
            foreach (var item in this.Items)
            {
                item.Parrent = this;
                item.Update(gameTime, extraData);
            }
        }


        protected override void Paint(Microsoft.Xna.Framework.GameTime gameTime, Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch, object extraData)
        {
            if (Background != null)
            {
                Vector2 pos = this.Position;
                Vector2 size = this.Size;
                Rectangle bound = new Rectangle((int)pos.X, (int)pos.Y, (int)size.X, (int)size.Y);
                Background.Draw(gameTime, spriteBatch, bound, Color.White);
            }
            foreach (var item in this.Items)
            {
                item.Draw(gameTime, spriteBatch, extraData);
            }
        }

        public static FormControl LoadFormControl(ContentManager content, XmlElement eleForm)
        {
            String Stringtype = eleForm.GetAttribute("type");
            Type type = Type.GetType(Stringtype);
            FormControl form = Activator.CreateInstance(type, new[] { content }) as FormControl;
            XmlElement xmlBackground = eleForm.SelectSingleNode("Sprite[@name='background']") as XmlElement;
            if (xmlBackground != null)
            {
                form.Background = new Sprite(content, xmlBackground);
            }
            XmlNodeList xmlButtons = eleForm.SelectNodes("ButtonControl");
            foreach (XmlElement ele in xmlButtons)
            {
                var button = new ButtonControl(content, ele);
                form.Items.Add(button);
            }
            int x, y;
            if (!int.TryParse(eleForm.GetAttribute("x"), out x))
            {
                x = 0;
            }
            if (!int.TryParse(eleForm.GetAttribute("y"), out y))
            {
                y = 0;
            }
            form.Position = new Microsoft.Xna.Framework.Vector2(x, y);
            int width, height;
            if (!int.TryParse(eleForm.GetAttribute("width"), out width))
            {
                width = 0;
            }
            if (!int.TryParse(eleForm.GetAttribute("height"), out height))
            {
                height = 0;
            }
            form.Size = new Vector2(width, height);
            int margin = 0;
            if (int.TryParse(eleForm.GetAttribute("margin"), out margin))
            {
                form.Margin = margin;
            }
            form.ValidateUI();
            return form;
        }


        public MainGame Game { get; set; }
    }
}
